package com.chenyi.data.structure.tree;

/**
 * @author chenyi
 * @className Tree
 * @date 2022/4/19 20:41
 */
public class BinaryTree<E> {

    public static void main(String[] args) {
        BinaryTree<Integer> binaryTree = new BinaryTree<>();
        Node root = new Node(10);
        Node nodeLeft = new Node(11);
        Node nodeRight = new Node(3);
        Node node4 = new Node(2);

        binaryTree.setRoot(root);
        root.setLeft(nodeLeft);
        root.setRight(nodeRight);
        nodeRight.setRight(node4);

        binaryTree.preOrder();
        System.out.println();
        binaryTree.infixOrder();
        System.out.println();
        binaryTree.nextOrder();
    }

    static class Node {
        Node left;
        Node right;
        int element;

        public Node(int element) {
            this.element = element;
        }

        public Node() {
        }

        @Override
        public String toString() {
            return "Node{" +
                    "element=" + element +
                    '}';
        }

        public void preOrder() {
            System.out.println(this);
            if (this.left != null) {
                this.left.preOrder();
            }
            if (this.right != null) {
                this.right.preOrder();
            }
        }

        public void infixOrder() {
            if (this.left != null) {
                this.left.infixOrder();
            }
            System.out.println(this);
            if (this.right != null) {
                this.right.infixOrder();
            }
        }

        public void nextOrder() {
            if (this.left != null) {
                this.left.nextOrder();
            }
            if (this.right != null) {
                this.right.nextOrder();
            }
            System.out.println(this);
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }
    }

    private Node root;

    public BinaryTree() {
    }

    public BinaryTree(int root) {
        this.root = new Node(root);
    }

    public BinaryTree(Node root) {
        this.root = root;
    }

    public void preOrder() {
        if (root != null) {
            root.preOrder();
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        }
    }

    public void nextOrder() {
        if (root != null) {
            root.nextOrder();
        }
    }

    public void add(int element) {
        Node node = new Node(element);
    }

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
}
